package com.ruoyi.eam.controller;

import java.util.HashMap;
import java.util.List;
import java.io.IOException;
import java.util.Map;
import java.util.Objects;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.eam.EamApplication;
import com.ruoyi.eam.constants.EamConstants;
import com.ruoyi.eam.doman.*;
import io.swagger.models.auth.In;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.eam.service.*;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 固定资产Controller
 *
 * @author ruoyi
 * @date 2024-06-17
 */
@RestController
@RequestMapping("/asset")
public class EamAssetController extends BaseController
{
    @Autowired
    private IEamAssetService eamAssetService;
    @Autowired
    private IEamTypeService eamTypeService;
    @Autowired
    private IEamLocationService eamLocationService;
    @Autowired
    private IDictDataService dictDataService;
    @Autowired
    private IEamSupplierService eamSupplierService;
    @Autowired
    private IUserService userService;
    @Autowired
    private RedissonClient redissonClient;





    /**
     * 查询固定资产列表
     */
    //@RequiresPermissions 这个注解用于指定一个方法或类需要某个特定的权限才能被访问。
    //@RequiresPermissions("system:asset:list")
    @GetMapping("/list")
    public TableDataInfo list(EamAsset eamAsset)
    {
        System.out.println("------eamAsset------ :");
        startPage();
        List<EamAsset> list = eamAssetService.selectEamAssetList(eamAsset);
        return getDataTable(list);
    }

    /**
     * 查询资产类别,存放位置,使用状态，供应商列表,经办人列表
     */
    //@RequiresPermissions("system:asset:typeList")
    @GetMapping("/typeList")
    public Map<String,Object> typeList(EamType eamType)
    {
        System.out.println("------typeList------ :");
        Map<String,Object> rm = new HashMap<>();
        startPage();
        List<EamType> typeList = eamTypeService.selectEamTypeList(null);
        List<EamLocation> locations = eamLocationService.selectEamLocationList(null);
        //数据字典 使用状态
        DictData dictData = new DictData();
        //创建用户
        User user = new User();
        //赋值仓库部门id，查询入库经办人
        user.setDeptId(EamConstants.DEPT_ID);
        dictData.setDictType(EamConstants.EAM_USESTATUS);
        //使用状态
        List<DictData> dictDataList = dictDataService.selectDictDataList(dictData);
        //供应商
        List<EamSupplier> supplierList = eamSupplierService.selectEamSupplierList(null);
        //经办人
        List<User> userList = userService.selectUserList(user);
        rm.put("typeList",typeList);
        rm.put("locationList",locations);
        rm.put("dictDataList",dictDataList);
        rm.put("supplierList",supplierList);
        rm.put("userList",userList);
        return rm;
    }


    /**
     * 导出固定资产列表
     */
    //@RequiresPermissions("system:asset:export")
    @Log(title = "固定资产", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, EamAsset eamAsset)
    {
        List<EamAsset> list = eamAssetService.selectEamAssetList(eamAsset);
        ExcelUtil<EamAsset> util = new ExcelUtil<EamAsset>(EamAsset.class);
        util.exportExcel(response, list, "固定资产数据");
    }

    /**
     * 获取固定资产详细信息
     */
    //@RequiresPermissions("system:asset:query")
    @GetMapping(value = "/{assetId}")
    public AjaxResult getInfo(@PathVariable("assetId") Long assetId)
    {
        return success(eamAssetService.selectEamAssetByAssetId(assetId));
    }

    /**
     * 新增固定资产
     */
    //@RequiresPermissions("system:asset:add")
    @Log(title = "新增固定资产", businessType = BusinessType.INSERT)
    @PostMapping("/addEam")
    public AjaxResult add(@RequestBody EamAsset eamAsset)
    {
        System.out.println("----addEam----"+eamAsset);
        //锁
        RLock lock = redissonClient.getLock("myinsertEamAssetCode");
        try{
            if (!lock.tryLock()) {
                return AjaxResult.error("操作过于频繁");
            }else{
                return toAjax(eamAssetService.insertEamAsset(eamAsset));
            }
        }finally {
            //释放
            lock.unlock();

        }
    }



    /**
     * 修改固定资产
     */
    //@RequiresPermissions("system:asset:edit")
    @Log(title = "固定资产", businessType = BusinessType.UPDATE)
    @PutMapping("edit")
    public AjaxResult edit(@RequestBody EamAsset eamAsset)
    {
        System.out.println("update");
        if(eamAssetService.updateEamAsset(eamAsset) > 0){
            return success();
        }
        return warn("修改失败");
    }

    /**
     * 删除固定资产
     */
    //@RequiresPermissions("system:asset:remove")
    @Log(title = "固定资产", businessType = BusinessType.DELETE)
	@DeleteMapping("/{assetIds}")
    public AjaxResult remove(@PathVariable Long[] assetIds)
    {
        for (Long assetId : assetIds) {
            System.out.println(assetId);
        }
        //逻辑删除
        return toAjax(eamAssetService.deleteEamAssetByAssetIds(assetIds));
    }

//    @RequiresPermissions("system:asset:edit")
    @Log(title = "固定资产", businessType = BusinessType.UPDATE)
    @PutMapping("editUseStatus")
    public AjaxResult editUseStatus(@RequestBody List<EamAsset> eamAssets)
    {
        return toAjax(eamAssetService.editUseStatus(eamAssets));
    }

    @GetMapping("/queryEamDateList")
    public TableDataInfo queryEamDateList(EamAsset eamAsset)
    {
        startPage();
        List<EamAsset> list = eamAssetService.queryEamDateList(eamAsset);
        return getDataTable(list);
    }

}
